const router = require("@koa/router")()
const jwt = require("jsonwebtoken")
const { localTime } = require("../../utils/index")
const model = require("../../model/api/Admin")
const _ = require("lodash");
const path = require("path");
const fs = require("fs");
const assert = require('http-assert')
router.get("/", async (ctx) => {
  ctx.body = "欢迎来到后台user模块"
})

router.post("/add", async (ctx) => {
  const params = _.omitBy(ctx.request.body, _.isNil);
  if (params.hasOwnProperty("user") && params.hasOwnProperty("passWord")) {
    try {
      const findRes = await model.find({ "user": params.user });
      if (findRes.length > 0) {
        ctx.body = {
          status: 200,
          message: "该用户已经存在",
          data: null
        }
      } else {
        const res = await model.create({ ...params, time: localTime() });
        if (res.__v === 0) {
          ctx.body = {
            status: 200,
            message: "创建成功",
            data: res
          }
        } else {
          ctx.body = {
            status: 0,
            message: "创建失败",
            data: null
          }
        }
      }

    } catch (error) {
      console.log(error)
      ctx.body = {
        status: 0,
        message: error._message,
        data: null
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "请输入密码或账号",
      data: null
    }
  }
})

router.delete("/delete", async (ctx) => {
  if (ctx.request.body.id) {
    try {
      const res = await model.findByIdAndDelete(ctx.request.body.id);
      if (res.__v === 0) {
        ctx.body = {
          status: 200,
          message: "删除成功",
          data: res
        }
      } else {
        ctx.body = {
          status: 0,
          message: "删除失败",
          data: res
        }
      }

    } catch (error) {
      console.log(error)
      ctx.body = {
        status: 0,
        message: "删除失败",
        data: null
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "缺少必要参数",
      data: null
    }

  }
})

router.put("/update", async (ctx) => {
  if (ctx.request.body.id) {
    try {
      const res = await model.findByIdAndUpdate(ctx.request.body.id, ctx.request.body.query, { new: true });
      if (res.__v === 0) {
        ctx.body = {
          status: 200,
          message: "修改成功",
          data: res
        }

      } else {
        ctx.body = {
          status: 0,
          message: "修改失败",
          data: res
        }
      }

    } catch (error) {
      console.log(error)
      ctx.body = {
        status: 0,
        message: "修改失败",
        data: error
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "缺少必要参数",
      data: null
    }

  }
})
router.get("/inquire", async (ctx) => {
  try {
    const res = await model.find();
    ctx.body = {
      status: 200,
      data: res,
      message: "成功"
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      status: 0,
      data: error,
      message: "失败"
    }
  }
})

router.get("/inquire/:aid", async (ctx) => {
  try {
    const res = await model.findById(ctx.params.aid);
    if (res) {
      ctx.body = {
        status: 200,
        data: res,
        message: "成功"
      }
    } else {
      ctx.body = {
        status: 0,
        data: null,
        message: "未查询到"
      }
    }

  } catch (error) {
    console.log(error)
    ctx.body = {
      status: 0,
      data: error,
      message: "失败"
    }
  }
})
router.post("/login", async (ctx) => {
  const { user, passWord } = ctx.request.body;
  try {
    const res = await model.findOne({ user }).select("+passWord");
    assert(res, 422, '用户不存在')
    const isValid = require('bcrypt').compareSync(passWord, res.passWord)
    assert(isValid, 422, '密码错误');
    var token = jwt.sign({ id: res._id }, "azhen");
    ctx.body = {
      status: 200,
      data: {
        token: token,
        id: res._id
      },
      message: "登录成功"
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      status: 0,
      data: null,
      message: error.message
    }
  }

})
module.exports = router